﻿using System.ComponentModel.Design;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using System.Windows.Forms;

namespace UtfprLibrary.Windows.Forms.ComponentModel
{
    [ComplexBindingProperties("DataSource", "DisplayMember")]
    [DefaultBindingProperty("DisplayMember")]
    internal class UtfprLookupFieldActionList : UtfprActionListBase
    {
        #region Properties

        private bool isQueryType
        {
            get
            {
                string baseTypeFullName = "UtfprLibrary.Windows.Forms.UtfprFormQueryBase";
                string name = linkedControl.FindForm().GetType().FullName;
                //string name = linkedControl.ParentForm.GetType().BaseType.FullName;
                return name == baseTypeFullName;
            }
        }

        private readonly UtfprLookupFieldBase linkedControl;

        [TypeConverter("System.Windows.Forms.Design.AdvancedBindingTypeConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        [Editor("System.Windows.Forms.Design.DataSourceListEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public object DataSource
        {
            get { return GetPropertyByName("DataSource").GetValue(linkedControl); }
            set { GetPropertyByName("DataSource").SetValue(linkedControl, value); }
        }

        [DefaultValue("")]
        [TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        [Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design", typeof(UITypeEditor))]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public object KeyMember
        {
            get { return GetPropertyByName("KeyMember").GetValue(linkedControl); }
            set { GetPropertyByName("KeyMember").SetValue(linkedControl, value); }
        }

        [TypeConverter("System.Windows.Forms.Design.AdvancedBindingTypeConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        [Editor("System.Windows.Forms.Design.DataSourceListEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public object LookupSource
        {
            get { return GetPropertyByName("LookupSource").GetValue(linkedControl); }
            set { GetPropertyByName("LookupSource").SetValue(linkedControl, value); }
        }

        [DefaultValue("")]
        [TypeConverterAttribute("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        [Editor(typeof(UtfprLookupSourceDataMemberFieldEditor), typeof(UITypeEditor))]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public object DisplayMember
        {
            get { return GetPropertyByName("DisplayMember").GetValue(linkedControl); }
            set { GetPropertyByName("DisplayMember").SetValue(linkedControl, value); }
        }

        [DefaultValue("")]
        [TypeConverterAttribute("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        [Editor(typeof(UtfprLookupSourceDataMemberFieldEditor), typeof(UITypeEditor))]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public object ValueMember
        {
            get { return GetPropertyByName("ValueMember").GetValue(linkedControl); }
            set { GetPropertyByName("ValueMember").SetValue(linkedControl, value); }
        }

        [Editor(typeof(UtfprQueryEditor), typeof(UITypeEditor))]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public object UtfprFormQueryType
        {
            get { return GetPropertyByName("UtfprFormQueryType").GetValue(linkedControl); }
            set { GetPropertyByName("UtfprFormQueryType").SetValue(linkedControl, value); }
        }


        [Editor(typeof(UtfprCrudEditor), typeof(UITypeEditor))]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public object UtfprFormCrudType
        {
            get { return GetPropertyByName("UtfprFormCrudType").GetValue(linkedControl); }
            set { GetPropertyByName("UtfprFormCrudType").SetValue(linkedControl, value); }
        }

        public int KeyMemberWidth
        {
            get { return (int)GetPropertyByName("KeyMemberWidth").GetValue(linkedControl); }
            set { GetPropertyByName("KeyMemberWidth").SetValue(linkedControl, value); }
        }

        public bool NonNumeric
        {
            get { return (bool)GetPropertyByName("NonNumeric").GetValue(linkedControl); }
            set { GetPropertyByName("NonNumeric").SetValue(linkedControl, value); }
        }

        #endregion

        public UtfprLookupFieldActionList(UtfprLookupFieldBase component, ControlDesigner designer)
            : base(component, designer)
        {
            linkedControl = (UtfprLookupFieldBase)LinkedControl;
        }

        public override DesignerActionItemCollection GetSortedActionItems()
        {
            var items = new DesignerActionItemCollection();

            items.Add(new DesignerActionHeaderItem("Appearence"));
            items.Add(new DesignerActionHeaderItem("Behavior"));
            items.Add(new DesignerActionHeaderItem("Lookup"));
            items.Add(new DesignerActionHeaderItem("Data"));

            items.Add(new DesignerActionPropertyItem(
                "KeyMemberWidth", "Key Member Width", "Appearence", "Set the Key Member With."));

            items.Add(new DesignerActionPropertyItem(
                "NonNumeric", "Non Numeric", "Behavior", "true if it is NonNumeric, otherwise false."));

            items.Add(new DesignerActionPropertyItem(
                "LookupSource", "Lookup Source", "Lookup", "Choose a Lookup Member."));

            items.Add(new DesignerActionPropertyItem(
                "DisplayMember", "Display Member", "Lookup", "Choose a Display Member."));

            items.Add(new DesignerActionPropertyItem(
                "ValueMember", "Value Member", "Lookup", "Choose a Value Member."));

            if (!isQueryType)
            {
                items.Add(new DesignerActionPropertyItem(
                    "DataSource", "Data Source", "Data", "Choose a Data Source."));

                items.Add(new DesignerActionPropertyItem(
                    "KeyMember", "Key Member", "Data", "Choose a Key Member."));
            }

            items.Add(new DesignerActionPropertyItem(
                "UtfprFormQueryType", "Query Type", "Data", "Choose a UtfprFormQuery Type."));

            items.Add(new DesignerActionPropertyItem(
                "UtfprFormCrudType", "CRUD Type", "Data", "Choose a UtfprFormCrud Type."));

            return items;
        }
    }
}
